import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableDescriptors;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService.put_args;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseIF {
	Configuration conf = null;

	private static String tab_global = "tab_global";
	private static String tab_user2id = "tab_user2id";
	private static String tab_id2user = "tab_id2user";

	HBaseIF() {
		conf = HBaseConfiguration.create();
	}

	public void createTable(String tbName, String colName, Integer ver)
			throws Exception {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tbName)) {
			admin.disableTable(tbName);
			admin.deleteTable(tbName);
		}
		HTableDescriptor tableDes = new HTableDescriptor(tbName);
		HColumnDescriptor hd = new HColumnDescriptor(colName);
		hd.setMaxVersions(ver);
		tableDes.addFamily(hd);
		admin.createTable(tableDes);
	}

	// tab_global param:userid
	// tab_user2id info:id
	// tab_id2user info:username info:password
	public void createTables() throws Exception {
		createTable(tab_global, "param", 1);
		Put put = new Put(Bytes.toBytes("row_userid"));
		long id = 0;
		put.add(Bytes.toBytes("parm"), Bytes.toBytes("userid"),
				Bytes.toBytes(id));
		HTable ht = new HTable(conf, tab_global);
		ht.put(put);

		createTable(tab_user2id, "info", 1);
		createTable(tab_id2user, "info", 1);
	}

	// 1.check user exist
	// 2.get new ID
	// 3. insert tab_user2id
	// 4.insert tab_id2user
	public boolean createNewUser(String username, String password)
			throws Exception {
		HTable htab_user2id = new HTable(conf, tab_user2id);
		HTable htab_id2user = new HTable(conf, tab_id2user);
		HTable htab_global = new HTable(conf, tab_global);
		if (htab_user2id.exists(new Get(username.getBytes()))) {
			return false;
		}
		long id = htab_global.incrementColumnValue(Bytes.toBytes("row_userid"),
				Bytes.toBytes("parm"), Bytes.toBytes("userid"), 1);
		Put put = new Put(username.getBytes());
		put.add("info".getBytes(), "id".getBytes(), Bytes.toBytes(id));
		htab_user2id.put(put);

		put = new Put(Bytes.toBytes(id));
		put.add("info".getBytes(), "username".getBytes(),
				Bytes.toBytes(username));
		put.add("info".getBytes(), "password".getBytes(),
				Bytes.toBytes(password));
		htab_id2user.put(put);

		return true;
	}

	/**
	 * 通过用户名取得ID
	 * 
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public long getIdByUsername(String username) throws Exception {
		try {
			HTable ht = new HTable(conf, tab_user2id);
			Result rs = ht.get(new Get(username.getBytes()));
			KeyValue kv = rs
					.getColumnLatest("info".getBytes(), "id".getBytes());
			return Bytes.toLong(kv.getValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 通过ID取得ID
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public String getUsernameById(long id) throws Exception {
		try {
			HTable ht = new HTable(conf, tab_id2user);
			Result rs = ht.get(new Get(Bytes.toBytes(id)));
			KeyValue kv = rs.getColumnLatest("info".getBytes(),
					"username".getBytes());
			return Bytes.toString(kv.getValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 检查密码
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public long checkPassword(String username, String password)
			throws Exception {
		try {
			long id = getIdByUsername(username);
			if (id > 0) {
				HTable ht = new HTable(conf, tab_id2user);
				Result rs = ht.get(new Get(Bytes.toBytes(id)));
				KeyValue kv = rs.getColumnLatest("info".getBytes(),
						"password".getBytes());
				return Bytes.toLong(kv.getKey());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static void main(String[] args) {
		HBaseIF hif = new HBaseIF();
		try {
			// hif.createTables();
			hif.createNewUser("user1", "pwd1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
